<?php

namespace App\Repositories;

use App\Jobs\Fun;
use App\Models\Admin\JobCompanyModel;
use App\Models\Admin\JobStaffModel;
use App\Models\JobsCompanysModel;
use App\Models\JobsModel;
use App\Models\User\UserModel;
use App\Models\UserStaffModel;
use App\Models\User\UserCompanysModel;

class JobsRepository
{
    protected $jobsCompanysModel;
    protected $jobsModel;
    protected $jobsStaffModel;
    protected $userModel;
    protected $userStaffModel;
    protected $userCompanysModel;

    public function __construct()
    {
        $this->jobsCompanysModel = new JobCompanyModel();
        $this->jobsModel = new JobsModel();
        $this->jobsStaffModel = new JobStaffModel();
        $this->userModel = new UserModel();
        $this->userStaffModel = new UserStaffModel();
     //   $this->userCompanysModel = new UserCompanysModel();
    }
/**zwp
 * 获取总记录
 * @param type $uid
 * @return object
 */
    public function getAllCount($uid){
        $data = $this->jobsStaffModel->where("uid",$uid)->get();
        return $data;
    }
    /**zwp
     * 获取用户的应聘列表
     * @param integer $uid
     * @param integer $page
     * @param integer $pageSize
     * @return array
     */
    
    public function getJobsLists($uid, $page, $pageSize)
    {
        $page = $page <= 0 ? 1 : $page;

        $offset = ($page - 1) * $pageSize;

        $jobs = $this->jobsStaffModel
            ->select('jobs_companys.id','jobs_companys.job','jobs_companys.num','jobs_companys.created_at', 'jobs_staffs.status')
            ->leftJoin('jobs_companys', 'jobs_staffs.origin_id', '=', 'jobs_companys.id')
            ->where(['jobs_companys.del' => 0, 'jobs_companys.status' => 2, 'jobs_staffs.del' => 0])
            ->where(['jobs_staffs.uid' => $uid])
            ->orderBy("jobs_companys.created_at","desc")
            ->skip($offset)
            ->take($pageSize)
            ->get();

        return $jobs->isEmpty() ? [] : $jobs->toArray();
    }

    public function findJobById($job_id){
        $data = $this->jobsModel->where("id",$job_id)->first();
        if(!$data){
            return "暂无";
        }
        return $data->name;
    }
    public function getZhaoPinDetail($id){
        $col = [
            "id","job","num","emolument","welfare","rest","education",
            "work_year","address","work_hour","detail","content",
            "contact_name","contact_telephone","email","sex","age_range","skill"
        ];
        $data = $this->jobsCompanysModel->where("id",$id)->first($col);
        if(!$data){
            return null;
        }
        return $data->toArray();
    }
    public function findCidByUid($uid){
        $cid = $this->userCompanysModel->where("uid",$uid)->first(["id"]);
        return $cid;
    }
    public function insertJob($data){
        $origin_id = $this->jobsCompanysModel->insertGetId($data);
        return $origin_id;
    }
    public function insertJobStaff($data){
        $flag = $this->jobsStaffModel->insert($data);
        return $flag;
    }

    /**
     * 根据公司ID，获取公司发布的招聘信息
     * @param array $cids
     * @return array
     */
    public function getJobs($cids = [])
    {
        if (empty($cids)) {
            return [];
        }
        $result = $this->jobsCompanysModel
            ->select('jobs_companys.*', 'jobs.name')
            ->leftJoin('jobs', 'jobs_companys.job', '=', 'jobs.id')
            ->where(['jobs_companys.del' => 0, 'jobs_companys.status' => 2, 'jobs.del' => 0])
            ->whereIn('jobs_companys.cid', $cids)
            ->orderBy('jobs_companys.created_at', 'desc')
            ->get();
        if ($result->isEmpty()) {
            return [];
        }
        return $result->toArray();
    }

    //添加招聘信息
    public function addJobsCompany($data)
    {
        $data['status'] = 2;
        $data['del'] = 0;
        $data['created_at'] = $data['updated_at'] = time();

        $model = $this->jobsCompanysModel;
        foreach ($data as $key => $value) {
            $model->setAttribute($key, $value);
        }
        return $model->save();
    }

    /**
     * 通过企业类型，获取部门
     */
    //获取所有部门
    public function getDepartment($column = '*', $type = 1)
    {
        return $this->jobsModel->newQuery()
            ->where(['pid' => 0, 'cate' => $type])
            ->select($column)
            ->get()
            ->toArray();
    }

    //获取部门分类下的所有员工
    public function getDepartmentStaff($department_id)
    {
        $jobs = $this->jobsModel->newQuery()
            ->where(['pid' => $department_id, 'del' => 0])
            ->select('id')
            ->get()->toArray();
        $jobIds = array_column($jobs, 'id');
        $modelArrl = $this->userStaffModel->newQuery()
            ->whereIn('job', $jobIds)
            ->select(['uid', 'realname as name', 'image'])
            ->get()->toArray();
        foreach ($modelArrl as $key=>$model){
           // var_dump(Fun::pImg($model['image']));exit;
            $modelArrl[$key]['image']=Fun::pImg($model['image']);
        }
           /* if (!$model['image'] || !file_exists(ltrim($model['image'], '/'))) {
                $model['image']=UserModel::defaultHead;
            }*/
        return $modelArrl;
    }

    //获取用户的应聘列表
    public function getUserJobs($uid, $page, $pageSize)
    {
        $page = $page <= 0 ? 1 : $page;

        $offset = ($page - 1) * $pageSize;

        $jobs = $this->jobsStaffModel
            ->select('jobs_companys.*', 'jobs_staffs.*')
            ->leftJoin('jobs_companys', 'jobs_staffs.origin_id', '=', 'jobs_companys.id')
            ->where(['jobs_companys.del' => 0, 'jobs_companys.status' => 2, 'jobs_staffs.del' => 0])
            ->where(['jobs_staffs.uid' => $uid])
            ->skip($offset)
            ->take($pageSize)
            ->get();

        return $jobs->isEmpty() ? [] : $jobs->toArray();
    }

    //获取用户的应聘列表
    public function getJobsDetail($id)
    {
        $jobDetail = $this->jobsStaffModel
            ->select('jobs_companys.*', 'jobs_staffs.*', 'jobs_staffs.status as job_status')
            ->leftJoin('jobs_companys', 'jobs_staffs.origin_id', '=', 'jobs_companys.id')
            ->where(['jobs_companys.del' => 0, 'jobs_companys.status' => 2, 'jobs_staffs.del' => 0])
            ->where(['jobs_staffs.id' => $id])
            ->first();

        return $jobDetail;
    }
}